package com.wanxi.servlet.user;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String hobby = req.getParameter("hobby");
        String enable = req.getParameter("enable");
//        3.封装到model
        UserVO userVO = UserVO.builder()
                .username(username)
                .sex(sex)
                .phone(phone)
                .email(email)
                .address(address)
                .start(start)
                .end(end)
                .hobby(hobby)
                .enable(Tool.strToInt(enable))
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .build();
//        4.处理逻辑关系
        UserService userService = new UserServiceImpl();
//        List<UserModel> modelList = userService.findAll(model);
//        int count = userService.count(model);
//        ResultModel resultModel= ResultModel.builder()
//                .list(modelList)
//                .count(count)
//                .build();
        ResultDTO resultModel = userService.findAll(userVO);
//        5.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
//        resp.getWriter().println(JSONObject.toJSONString(count));

    }
}
